package com.liangzhong.model.bag
{
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.bag.vo.BagInfoVo;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.type.GoodsType;
	import com.liangzhong.model.type.MaterialType;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;

	/**
	 * 背包数据管理器 
	 * @author lake
	 * 
	 */	
	public class BagDataManager extends EventDispatcher
	{
		/**
		 *仓库信息 
		 */		
		public var bagInfo				:BagInfoVo;
		/**
		 *背包商品列表 
		 */		
		public var goodsList			:Array = [];
		
		public function BagDataManager()
		{
			bagInfo = new BagInfoVo();
		}
		/**
		 *根据类型得到物品 
		 * @param type
		 * @return 
		 * 
		 */		
		public function getGoodsListByType(type :int):Array
		{
			if(type == GoodsType.ALL)
			{
				return goodsList;
			}
			var res :Array = [];
			for each(var goods :GoodsVo in goodsList)
			{
				if(goods.type == type)
				{
					res.push(goods);
				}
				if(type == GoodsType.EQUIP 
					&& (goods.type == GoodsType.ATTRIBUTE 
						|| goods.type == GoodsType.FOOTWEAR))
				{
					res.push(goods);
				}
				if(type == GoodsType.OTHER 
					&& (goods.type == GoodsType.DRUG))
				{
					res.push(goods);
				}
			}
			return res;
		}
		/**
		 * 获取商品列表 通过两种类型
		 * @param type  商品主类型 如 GoodsType.OHTER
		 * @param ntype 商品小类型 如 OtherGoodType.PHYSICAL 获取体力卡
		 * @return 
		 */		
		public function getGoodsListByTwoTypes(type:int,
											   ntype:int = 0):Array
		{
			var res 	:Array = getGoodsListByType(type);
			if(ntype == GoodsType.ALL)
			{
				return res;
			}
			var nres 	:Array = [];
			for each(var goods :GoodsVo in res)
			{
				if(goods.ntype == ntype)
				{
					nres.push(goods);
				}
			}
			return nres;
		}
		/**
		 *解析背包物品信息 
		 * @param data
		 * 
		 */		
		public function parsePropInfo(data :Object):void
		{
			var objList :Array = data as Array;
			if(objList == null)
			{
				return;
			}
			var goods 	:GoodsVo;
			var probid 	:int;//道具ID 
			var id		:int;//对应表的ID：如果ntype是5，就是材料卡
			var ntype	:int;//道具类型：
			var locked	:int;//是否加锁
			for each(var obj :Object in objList)
			{
				probid 		= obj.cellid;
				id 			= obj.id;
				ntype 		= obj.ntype;
				locked 		= obj.locked;
				addPropByTypeAndId(probid,ntype,id,locked);
			}
			this.dispatchEvent(new Event(Event.CHANGE));
			//测试
			/*addPropByTypeAndId(11,GoodsType.PLAYERCARD,100,1);
			addPropByTypeAndId(12,GoodsType.PLAYERCARD,102,1);
			addPropByTypeAndId(13,GoodsType.PLAYERCARD,103,1);
			addPropByTypeAndId(14,GoodsType.PLAYERCARD,104,1);
			addPropByTypeAndId(15,GoodsType.PLAYERCARD,105,1);*/
			
			/*addPropByTypeAndId(13,GoodsType.ATTRIBUTE,20,1);
			addPropByTypeAndId(14,GoodsType.ATTRIBUTE,30,1);
			addPropByTypeAndId(15,GoodsType.ATTRIBUTE,40,1);
			addPropByTypeAndId(15,GoodsType.ATTRIBUTE,2,1);
			addPropByTypeAndId(15,GoodsType.ATTRIBUTE,10,1);
			addPropByTypeAndId(15,GoodsType.ATTRIBUTE,25,1);*/
			
			/*addPropByTypeAndId(13,GoodsType.DRUG,1,1);
			addPropByTypeAndId(14,GoodsType.DRUG,2,1);
			addPropByTypeAndId(15,GoodsType.DRUG,3,1);
			addPropByTypeAndId(15,GoodsType.DRUG,4,1);
			addPropByTypeAndId(15,GoodsType.DRUG,5,1);
			addPropByTypeAndId(15,GoodsType.DRUG,6,1);*/
			
			/*addPropByTypeAndId(13,GoodsType.FOOTWEAR,1,1);
			addPropByTypeAndId(14,GoodsType.FOOTWEAR,2,1);
			addPropByTypeAndId(15,GoodsType.FOOTWEAR,3,1);*/
			
			/*addPropByTypeAndId(13,GoodsType.OTHER,1,1);
			addPropByTypeAndId(14,GoodsType.OTHER,2,1);
			addPropByTypeAndId(15,GoodsType.OTHER,3,1);
			addPropByTypeAndId(14,GoodsType.OTHER,4,1);
			addPropByTypeAndId(15,GoodsType.OTHER,5,1);*/
		}
		/**
		 *	改变物品 
		 * @param pid
		 * @param type 1 增加 2 删除
		 * @param ntype 道具类型
		 * @param id	道具表中id
		 */		
		public function changeGoods(pid :int,
									type:int,
									ntype:int,
									id:int,
									locked:int = 1):void
		{
			switch(type)
			{
				case 1:
				{
					addPropByTypeAndId(pid,ntype,id);
					break;
				}
				case 2:
				{
					deletePropByPid(pid);
					break;
				}
			}
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		/**
		 *增加道具 通过类型 和id 
		 * @param pid 唯一id
		 * @param ntype 类型
		 * @param id 对应表中的id
		 * 
		 */		
		public function addPropByTypeAndId(pid :int,
										   ntype:int,
										   id:int,
										   locked:int = 1):void
		{
			var goods :GoodsVo = AppData.getInstance().goodsManager.getGoodsByTypeAndId(ntype,id);
			if(goods)
			{
				goods = goods.clone();
				goods.pid = pid;
				goods.locked = locked;
				goodsList.push(goods);
			}
		}
		/**
		 *删除道具通过pid 
		 * @param pid
		 * 
		 */		
		public function deletePropByPid(pid :int):void
		{
			var i :int = 0;
			for each(var goods :GoodsVo in goodsList)
			{
				if(goods.pid == pid)
				{
					goodsList.splice(i,1);
				}
				i ++;
			}
		}
		/**
		 *获取球衣合成中物品 
		 * @return 
		 * 
		 */		
		public function getClothCombineList():Array
		{
			var res :Array = [];
			for each(var goods :GoodsVo in goodsList)
			{
				if(goods.type == GoodsType.MATERIAL 
					&& (goods.ntype == MaterialType.STAR_SOUL 
						|| goods.ntype == MaterialType.TRIKOT_CHIP))
				{
					res.push(goods);
				}
			}
			return res;
		}
		/**
		 *其他类 最前 
		 * 
		 */		
		public function otherSortFirst():void
		{
			goodsList.sortOn("ntype",Array.DESCENDING | Array.NUMERIC);
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		/**
		 *球员卡最前 
		 * 
		 */		
		public function playerCardFirst():void
		{
			goodsList.sortOn(["ntype","color"],[Array.NUMERIC,Array.DESCENDING | Array.NUMERIC]);
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		/**
		 * 根据球星id获取背包对应的球员商品 
		 * @param starId
		 * @return 
		 * 
		 */		
		public function getStarsGoodsByStarId(starId :int):Array
		{
			var res :Array = [];
			var starList :Array = getGoodsListByType(GoodsType.PLAYERCARD);
			for each(var goods :GoodsVo in starList)
			{
				if(goods.gid == starId)
				{
					res.push(goods);
				}
			}
			return res;
		}
	}
}